package org.sgx.gwteditors.testgallery.gallery;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.sgx.gwteditors.client.editor.props.AbstractPropertyHaver;
import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.editor.props.PropertyInfo;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.basetypes.Color;
import org.sgx.gwteditors.client.impl1.basetypes.InRangeDouble;
import org.sgx.gwteditors.client.impl1.basetypes.SelectionList;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor3;
import org.sgx.gwteditors.client.impl1.complex.PropertyHaverEditor2;
import org.sgx.gwteditors.client.impl1.edbean.EditableBean;
import org.sgx.gwteditors.client.util.LWOMap;
import org.sgx.gwteditors.client.util.TestUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ExternalTextResource;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * try to create an editor of editors, this is let the user define a PropertyHaver using an editor. In this example we accomplish that using a ListEditor and a PropertyHaver
 * editor.
 * 
 * @author sg
 * 
 */
public class EditorOfEditorTest1 implements Test {


	/*---begintestmaincode---*/

	private Panel parent;
	private Array1DEditor3<PHProperty>  objEd;
	@Override
	public void test(final Panel parent) {
		this.parent=parent;
		new EditorFramework1().start();
		parent.add(new HTML("<p>Please, first define your Object Type with the following editor: </p>"));
		// we will say a PH is simple a list of PHProperty
		List<PHProperty> objDef = new LinkedList<PHProperty>();
		objDef.add(PHProperty.create());
		this.objEd = new Array1DEditor3(new Array1DEditor3.ItemCreator<PHProperty>() {
			@Override
			public PHProperty createNewItem() {
				return PHProperty.create();
			}
		});
		objEd.load(objDef);
		parent.add(objEd.getWidget());

		Button showEditorButton = new Button("Show generated editor", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				doShowEditor();
			}
		});
		parent.add(showEditorButton); 
	}

	protected void doShowEditor() {
		PropertyHaver ph = buildRealPH(objEd.flush()); 
		PropertyHaverEditor2<PropertyHaver> ed = new PropertyHaverEditor2<PropertyHaver>(); 
//		System.out.println("PHProperty "+ph+" - length. "+objEd.flush().size());
		ed.load(ph); 
//		System.out.println(ph.propertyGet("color"));
		parent.add(ed.getWidget()); 
	}

	private PropertyHaver buildRealPH(List<PHProperty> l) {
		AbstractPropertyHaver ph = new AbstractPropertyHaver();
//		System.out.println("PropertyInfo: "+l.size());
		LWOMap<String, PropertyInfo> props = ph.getProperties(); //new LWOMap<String, PropertyInfo>(); 
		for(PHProperty p : l) {			
			String name = p.getName(); 
			String type = p.getType().getSelection().get(0);
			PropertyInfo info = new PropertyInfo(name, type, "", "", null, null);			
			props.put(p.getName(), info);
			if(type.equals(EditorFramework1.TYPE_COLOR))
				ph.propertySet(name, new Color("edaa25"));
			if(type.equals(EditorFramework1.TYPE_STRING))
				ph.propertySet(name, "string");
			if(type.equals(EditorFramework1.TYPE_DOUBLE))
				ph.propertySet(name, 3.14);
			if(type.equals(EditorFramework1.TYPE_INTEGER))
				ph.propertySet(name, 3);
			if(type.equals(EditorFramework1.TYPE_BOOLEAN))
				ph.propertySet(name, true);
		}
		
		return ph;
	}

	// The bean we want to edit
	/**
	 * this represents an object property
	 * 
	 * @author sg
	 * 
	 */
	public static abstract class PHProperty implements EditableBean {
		String name, description, label, editorId, config;
		SelectionList<String> type;

		public static PHProperty create() {
			PHProperty p = com.google.gwt.core.shared.GWT.create(PHProperty.class);
			p.setName("color");
			p.setDescription("");
			p.setLabel("color");
			p.setType(new SelectionList<String>(EditorFramework1.ALL_EDITOR_TYPES, EditorFramework1.TYPE_COLOR));
			p.getProperties().reorder(new String[] { "name", "type" });
			return p;
		}

		@Override
		public String toString() {
			return name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public String getEditorId() {
			return editorId;
		}

		public void setEditorId(String editorId) {
			this.editorId = editorId;
		}

		public String getConfig() {
			return config;
		}

		public void setConfig(String config) {
			this.config = config;
		}

		public SelectionList<String> getType() {
			return type;
		}

		public void setType(SelectionList<String> type) {
			this.type = type;
		}

	}

	/*---endtestmaincode---*/

	// test stuff
	@Override
	public String getName() {
		return "EditorOfEditorTest1";
	}

	@Override
	public Map<String, ExternalTextResource> getJavaSources() {
		return TestUtil.getResource(getName(), TestResources.INSTANCE.BeanEditorTest2());
	}

	@Override
	public String[] getTags() {
		return new String[] {};
	}

	@Override
	public String getDescription() {
		return "try to create an editor of editors, this is let the user define a PropertyHaver using an editor. In this example we accomplish that using a ListEditor and a PropertyHaver editor. ";
	}
}
